package com.geek.service.impl;

import com.geek.service.TopicService;
import com.github.pagehelper.PageHelper;
import com.geek.bean.data.CategoryListData;
import com.geek.bean.data.TopicData;
import com.geek.bean.data.TopicListData;
import com.geek.bean.data.TopicRelatedData;
import com.geek.mapper.MarketGoodsMapper;
import com.geek.mapper.MarketTopicMapper;
import com.geek.model.MarketGoods;
import com.geek.model.MarketGoodsExample;
import com.geek.model.MarketTopic;
import com.geek.model.MarketTopicExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class TopicServiceImpl implements TopicService {
    @Autowired
    MarketTopicMapper topicMapper;
    @Autowired
    MarketGoodsMapper goodsMapper;

    @Override
    public List<MarketTopic> list(int page, int topicLimit) {
        PageHelper.startPage(page, topicLimit);
        MarketTopicExample example = new MarketTopicExample();
        example.createCriteria().andDeletedEqualTo(false);
        example.setOrderByClause("add_time desc");
        List<MarketTopic> marketTopics = topicMapper.selectByExample(example);
        return marketTopics;
    }

    
    //专题列表
    @Override
    public Map<String, Object> listData(Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        Map<String, Object> map = new HashMap<>();
        List<TopicListData> list = new ArrayList<>();
        List<MarketTopic> listAll = list(page, limit);
        Integer total = listAll.size();
        Integer pages = (total % limit > 0) ? (total / limit) + 1 : total / limit;
        for (MarketTopic marketTopic : listAll) {
            TopicListData topicListData = TopicListData.builder()
                    .id(marketTopic.getId())
                    .picUrl(marketTopic.getPicUrl())
                    .price(marketTopic.getPrice())
                    .readCount(marketTopic.getReadCount())
                    .subtitle(marketTopic.getSubtitle())
                    .title(marketTopic.getTitle())
                    .build();
            list.add(topicListData);
        }
        map.put("limit", limit);
        map.put("list", list);
        map.put("page", page);
        map.put("pages", pages);
        map.put("total", total);
        return map;
    }

    
    //专题详情
    @Override
    public TopicData detail(Integer id) {
        List<CategoryListData> categoryListData;
        MarketTopic marketTopic = topicMapper.selectByPrimaryKey(id);
        //String[] goods = marketTopic.getGoods();
        String[] goodsIds = marketTopic.getGoods();
        if (goodsIds.length == 0) {
            categoryListData = new ArrayList<>();
        } else {
            MarketGoodsExample example = new MarketGoodsExample();
            example.createCriteria().andDeletedEqualTo(false)
                    .andGoodsSnIn(List.of(goodsIds));
            example.setOrderByClause("add_time desc");
            List<MarketGoods> goodsList = goodsMapper.selectByExample(example);
            categoryListData = goodsList.stream().map(marketGoods -> CategoryListData.builder()
                    .brief(marketGoods.getBrief())
                    .counterPrice(marketGoods.getCounterPrice())
                    .id(marketGoods.getId())
                    .isHot(marketGoods.getIsHot())
                    .isNew(marketGoods.getIsNew())
                    .name(marketGoods.getName())
                    .picUrl(marketGoods.getPicUrl())
                    .retailPrice(marketGoods.getRetailPrice())
                    .build()).collect(Collectors.toList());
        }
        
        TopicData build = TopicData.builder()
                .goods(categoryListData)
                .topic(marketTopic)
                .build();
        return build;
    }

    
    //专题相关推荐
    @Override
    public TopicRelatedData related(Integer id) {
        MarketTopicExample example = new MarketTopicExample();
        example.createCriteria().andDeletedEqualTo(false)
                .andIdNotEqualTo(id);
        example.setOrderByClause("add_time desc");
        List<MarketTopic> marketTopics = topicMapper.selectByExample(example);
        Integer total = marketTopics.size();
        Integer page = 1;
        Integer limit = 4;
        Integer pages = (total % limit) > 0 ? (total / limit) + 1 : (total / limit);
        List<MarketTopic> list = new ArrayList<>();
        list.add(marketTopics.get(0));
        list.add(marketTopics.get(1));
        list.add(marketTopics.get(2));
        list.add(marketTopics.get(3));
        TopicRelatedData data = TopicRelatedData.builder()
                .limit(limit)
                .list(list)
                .page(page)
                .pages(pages)
                .total(total)
                .build();
        return data;
    }

    @Override
    public List<MarketTopic> topicList(int page, int topicLimit) {
        MarketTopicExample example = new MarketTopicExample();
        PageHelper.startPage(page, topicLimit);
        example.createCriteria().andDeletedEqualTo(false);
        example.setOrderByClause("add_time desc");
        return topicMapper.selectByExample(example);
    }
}
